home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / demos / GL / flight / collision.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  3.0 KB  |  123 lines

  1. /*
  2.  * Copyright 1984-1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18.  
  19. #include <gl.h>
  20. #include <stdio.h>
  21. #include <fcntl.h>
  22.  
  23. #include "collision.h"
  24.  
  25.  
  26. /*
  27.  * collide_tri() returns true if the given point has a y value less than the
  28.  * y value of the given triangle at the given x and z location.
  29.  *
  30.  *    A-------B
  31.  *    | P    /
  32.  *    |    /
  33.  *    |  /
  34.  *    |/
  35.  *    C
  36.  */
  37. int collide_tri(px, py, pz, ax, bx, az, cz, aelv, belv, celv)
  38.     float px, py, pz, ax, bx, az, cz, aelv, belv, celv;
  39. {
  40.     float delta1, elv1, delta2, elv2;
  41.  
  42.     delta1 = (ax - px) / (ax - bx);
  43.     elv1 = aelv - ((aelv - belv) * delta1);
  44.     delta2 = (az - pz) / (az - cz);
  45.     elv2 = elv1 - ((aelv - celv) * delta2);
  46.  
  47.     if (py < elv2)
  48.     return(TRUE);
  49.     else
  50.     return(FALSE);
  51. }
  52.  
  53.  
  54.  
  55. /*
  56.  * collide_grid() returns true if the given point is lower than the given grid
  57.  * at the point's x and z location.
  58.  */
  59. int collide_grid(float px, float py, float pz, grid_t *g)
  60. {
  61.     int gx, gz;
  62.     float rpx, rpz;
  63.  
  64.     if (px > g->xmin && px < g->xmax &&
  65.     pz > g->zmin && pz < g->zmax)
  66.     {
  67.     gx = (px - g->xmin) / g->stepsize;
  68.     gz = (pz - g->zmin) / g->stepsize;
  69.     rpx = px - gx * g->stepsize - g->xmin;
  70.     rpz = pz - gz * g->stepsize - g->zmin;
  71.     if (rpx + rpz < g->stepsize)
  72.         return(collide_tri(rpx, py, rpz, 0.0, g->stepsize, 0.0, g->stepsize,
  73.                g->elv[gx][gz], g->elv[gx+1][gz], g->elv[gx][gz+1]));
  74.     else
  75.         return(collide_tri(rpx, py, rpz, g->stepsize, 0.0, g->stepsize, 0.0,
  76.                g->elv[gx+1][gz+1], g->elv[gx][gz+1], g->elv[gx+1][gz]));
  77.     }
  78.  
  79.     return(FALSE);
  80. }
  81.  
  82.  
  83.  
  84. /*
  85.  *  Read a grid file
  86.  */
  87. grid_t *read_grid(char *fname)
  88. {
  89.     int x, z;
  90.     int fd;
  91.     grid_t *g;
  92.  
  93.     if ((fd = open(fname, O_RDONLY)) == -1)
  94.     {
  95.     fprintf(stderr, "can't open \"%s\"\n", fname);
  96.     perror("read");
  97.     }
  98.  
  99.     g = (grid_t *)malloc(sizeof(grid_t));
  100.  
  101.     read(fd, &g->xsize, sizeof(int));
  102.     read(fd, &g->zsize, sizeof(int));
  103.  
  104.     g->elv = (float **)malloc(sizeof(float *) * g->xsize+1);
  105.     for (x=0; x <= g->xsize; x++)
  106.     g->elv[x] = (float *)malloc(sizeof(float) * g->zsize+1);
  107.  
  108.     for (z=0; z <= g->zsize; z++)
  109.     for (x=0; x <= g->xsize; x++)
  110.     {
  111.         read(fd, &g->elv[x][z], sizeof(float));
  112.         g->elv[x][z] *= 2000.0;
  113.     }
  114.  
  115.     close(fd);
  116.     g->stepsize = 2000.0;
  117.     g->xmin = g->zmin = 0.0;
  118.     g->xmax = g->xsize * 2000.0;
  119.     g->zmax = g->zsize * 2000.0;
  120.     return(g);
  121. }
  122.  
  123.